import { Controller, Get, Inject } from "@nestjs/common";
import { AppService } from "./app.service";
import { MessagePattern } from "@nestjs/microservices";
import { Observable } from "rxjs";
import { RBAC } from "../../../config/config";
import { SysIntervalMsgType } from "../../../types/types";
import { PrismaService } from "@app/prisma";
import { NoNeedLogin } from "../../../libs/decorator/no-need-login/no-need-login.decorator";
import { RequirePermission } from "../../../libs/decorator/require-permission/require-permission.decorator";
import { NatsClientService } from "@app/nats-client";

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    private prisma: PrismaService,
    @Inject(NatsClientService)
    private natsClient: NatsClientService,
  ) {}

  @Get()
  getHello(): string {
    return "AAAA";
  }

  @Get("/get")
  getGet(): string {
    return "get";
  }

  @Get("/set")
  getSet(): string {
    return "set";
  }

  @Get("/delete")
  getDelete(): string {
    return "delete";
  }

  @Get("/micro")
  @NoNeedLogin()
  async testMicoService(): Promise<any> {
    console.log("收到请求");
    try {
      const ans: Observable<any> = this.natsClient.send("sum", [1, 2, 3, 4]);
      ans.subscribe({
        next: (value) => console.log(value), // 每次发出值时的处理函数
        complete: () => {
          console.log("Observable completed");
          return "finish";
        }, // 完成时的处理函数
        error: (err) => console.error("Something went wrong: " + err), // 错误处理函数
      });
      console.log(ans);
    } catch (err) {
      console.log(err);
    }
  }

  @MessagePattern(SysIntervalMsgType.RECEIVE_HARDWARE_MSG_EVENT)
  handleMessage(data: any) {
    console.log("收到订阅的硬件消息:", data);
  }

  @MessagePattern(SysIntervalMsgType.SYSTEM_MSG_EVENT)
  handleGatewayMsg(data: any): any {
    console.log("收到网关发来的消息:", data);
    return "ack";
  }
}
